package com.xc.freeapp.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xc.freeapp.dao.DeptInfoDao;
import com.xc.freeapp.dao.UserInfoDao;
import com.xc.freeapp.dao.dataSource.DBContextHolder;
import com.xc.freeapp.entity.DeptInfo;
import com.xc.freeapp.entity.UserInfo;
import com.xc.freeapp.service.DeptService;
import com.xc.freeapp.util.StringUtil;

/**
 * 数据处理具体实现
 * 
 * @author ZhouXinjie
 * 
 */

@Service
public class DeptServiceImpl implements DeptService {

	@Autowired
	private DeptInfoDao deptDao;
	@Autowired
	private UserInfoDao userInfoDao;

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public List<DeptInfo> queryDeptBySuperId(String superId) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return deptDao.queryDeptBySuperId(superId);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public DeptInfo getDeptByDeptId(String deptId) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return deptDao.getDeptByDeptId(deptId);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int insertDept(DeptInfo dept) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		// 插入节点前，修改父级节点为非叶子节点
		DeptInfo father = deptDao.getDeptByDeptId(dept.getSuperId());
		if (father.getIfLeaf().equals("1")) {
			father.setIfLeaf("0");
			deptDao.updateDept(father);
		}
		// 如果设置为部门经理，同时修改用户的所在部门
		if (StringUtil.isNotEmpty(dept.getDeptManager())) {
			UserInfo userInfo = userInfoDao.getUser(dept.getDeptManager());
			userInfo.setDeptId(dept.getDeptId());
			userInfoDao.updateUser(userInfo);
		}
		return deptDao.insertDept(dept);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int updateDept(DeptInfo dept) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		// 如果设置为部门经理，同时修改用户的所在部门
		if (StringUtil.isNotEmpty(dept.getDeptManager())) {
			UserInfo userInfo = userInfoDao.getUser(dept.getDeptManager());
			userInfo.setDeptId(dept.getDeptId());
			userInfoDao.updateUser(userInfo);
		}
		return deptDao.updateDept(dept);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int deleteDept(String deptId) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		int result = 0;
		// 检测该节点是否有子节点
		List<DeptInfo> list = this.queryDeptBySuperId(deptId);
		if (list != null && list.size() > 0) {
			result = 2;
		} else {
			DeptInfo dept = deptDao.getDeptByDeptId(deptId);
			DeptInfo father = deptDao.getDeptByDeptId(dept.getSuperId());
			result = deptDao.deleteDept(deptId);
			// 删除后，检测父级节点是否还有子节点
			List<DeptInfo> child = this.queryDeptBySuperId(father.getDeptId());
			if (child == null || child.size() == 0) {
				// 如果没有子节点，则设置为叶子节点
				father.setIfLeaf("1");
				deptDao.updateDept(father);
			}
			// 同时删除用户的所在部门
			userInfoDao.deleteDeptId(deptId);
		}
		return result;
	}

	/**
	 * 新增时生成deptId
	 * 
	 * @return deptId
	 */
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public String getMaxPrimaryKey() {
		DBContextHolder.switchDataSourceRW();//切换数据源
		String MaxId = deptDao.getMaxPrimaryKey();
		int num = Integer.parseInt(MaxId.replaceFirst("d", ""));
		int newNum = num + 1;
		StringBuffer deptId = new StringBuffer("d");
		deptId.append(newNum);
		return deptId.toString();
	}

	/**
	 * 新增时生成deptNo
	 * 
	 * @param superId
	 * @return deptNo
	 */
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public String getMaxDeptNo(String superId) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		DeptInfo superDept = this.getDeptByDeptId(superId);
		StringBuffer deptNo = new StringBuffer(superDept.getDeptNo());
		// 检测上级节点是否有子节点
		List<DeptInfo> list = this.queryDeptBySuperId(superId);
		if (list != null && list.size() > 0) {
			String MaxDeptNo = deptDao.getMaxDeptNo(superId);
			String num = MaxDeptNo.substring(superDept.getDeptNo().length());
			int newNum = Integer.parseInt(num) + 1;
			deptNo.append(newNum);
		} else {
			deptNo.append("01");
			// 子节点最多99个
		}
		return deptNo.toString();
	}
	
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<DeptInfo> getDeptByDeptManager(String userId) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return deptDao.getDeptByDeptManager(userId);
	}
	

}
